//
//  HomeADView.m
//  globalcarbusiness
//
//  Created by dongzhenyu on 2017/8/26.
//  Copyright © 2017年 dongzhenyu. All rights reserved.
//

#import "HomeADView.h"
#import "Masonry.h"

#define HEIGHT_Adaptation [UIScreen mainScreen].bounds.size.height/667.0
#define WINDOW_HEIGHT [UIScreen mainScreen].bounds.size.height
#define WINDOW_WIDTH [UIScreen mainScreen].bounds.size.width

@interface HomeADView()

@property (nonatomic, strong) UIWindow *keyWindow;

@property (nonatomic, strong) UIButton *closeBtn;

@property (nonatomic, strong) UIButton *jiaruButton;

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UITapGestureRecognizer *ges;
@end

@implementation HomeADView

- (void)showAlert
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"duihuan" ofType:@"png"]]];
    imageView.userInteractionEnabled = YES;
    imageView.tag = 100;
    imageView.alpha = 0;
    imageView.transform = CGAffineTransformMakeScale(0.3, 0.3);
    self.imageView = imageView;
    [self.keyWindow addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.keyWindow);
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn addTarget:self action:@selector(hideAlert:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.keyWindow).offset(-55*HEIGHT_Adaptation);
        make.size.mas_equalTo(CGSizeMake(20*HEIGHT_Adaptation, 20*HEIGHT_Adaptation));
        make.top.equalTo(self.keyWindow).offset(205*HEIGHT_Adaptation);
    }];
    
    UIButton *jiaruBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [jiaruBtn addTarget:self action:@selector(jiaru:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:jiaruBtn];
    [jiaruBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120*HEIGHT_Adaptation, 120*HEIGHT_Adaptation));
        make.right.equalTo(self.keyWindow).offset(-55*HEIGHT_Adaptation);
        make.top.equalTo(self.keyWindow).offset(205*HEIGHT_Adaptation);
    }];
    
    
    UIButton *jiaruRenbao = [UIButton buttonWithType:UIButtonTypeCustom];
    [jiaruRenbao addTarget:self action:@selector(jiaruRenbao:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:jiaruRenbao];
    [jiaruRenbao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120*HEIGHT_Adaptation, 120*HEIGHT_Adaptation));
        make.right.equalTo(self.keyWindow).offset(-195*HEIGHT_Adaptation);
        make.top.equalTo(self.keyWindow).offset(205*HEIGHT_Adaptation);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        imageView.alpha = 1;
        imageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAlert:)];
    self.ges = tap;
    [self.keyWindow addGestureRecognizer:tap];
  
}

- (void)hideAlert:(UIButton *)sender
{
    UIImageView *imageView = [self.keyWindow viewWithTag:100];
    [UIView animateWithDuration:0.25 animations:^{
        imageView.alpha = 0;
        imageView.transform = CGAffineTransformMakeScale(0.3, 0.3);
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
        [self.keyWindow removeGestureRecognizer:self.ges];
        self.ges = nil;
    }];
    
}

- (void)jiaru:(UIButton *)sender
{
    [self hideAlert:sender];
    if (self.clickHandler) {
        self.clickHandler(YES);
    }
    
}

- (void)jiaruRenbao:(UIButton *)sender
{
    [self hideAlert:sender];
    if (self.clickHandler) {
        self.clickHandler(NO);
    }
}

- (UIWindow *)keyWindow
{
    if (!_keyWindow) {
        _keyWindow = [UIApplication sharedApplication].keyWindow;
    }
    return _keyWindow;
}


@end
